package com.taobao.org.controller;
import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.PageList;
import com.taobao.org.domain.Employee;
import com.taobao.org.domain.Employee;
import com.taobao.org.query.EmployeeQuery;
import com.taobao.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/emp")
@Api(value = "员工接口类",description = "员工接口的CRUD操作")
public class EmployeeController {

    @Autowired
    private IEmployeeService EmployeeService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> findAll(){
        return EmployeeService.loadAll();
    }
    /**
     * 根据id查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Employee findOne(@PathVariable("id") Long id){
        return EmployeeService.loadById(id);
    };


    /**
     * 根据shopId查询所有的店蒲员工
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "根据shopid查询" )
    public List<Employee> findByShopId(@PathVariable("shopId") Long shopId){
        return EmployeeService.findByShopId(shopId);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public JsonResult DeleteByID(@PathVariable("id") Long id){
        try {
            EmployeeService.remove(id);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 添加或修改操作
     * @param Employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public JsonResult updateOrAdd(@RequestBody Employee Employee ){
        try {
            if (Employee.getId() == null){
                EmployeeService.save(Employee);
            }else {
                EmployeeService.update(Employee);
            }
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     *
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给EmployeeQuery
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery query){
        try {
            return EmployeeService.pageQuery(query);
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<Employee>();
        }
    }
    /**
     * 批量删除
     * @param
     * @return
    */
    @PatchMapping()
    @ApiOperation(value = "删除接口")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            EmployeeService.patchDel(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
}
